package main

import (
	"fmt"
	"gin/model"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func main() {
	dsn := "root:e4fc1c81f7b78285@tcp(10.168.30.15:3306)/sync_test?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	db = db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Info)})

	// 自动迁移模式创建表结构 (根据结构体定义)
	//db.AutoMigrate(&User{})

	// 插入数据
	//user := User{Name: "test_user", Email: "test@example.com"}
	//re := db.Create(&user)
	//fmt.Println(re)
	//fmt.Println(user)

	// 查询数据
	var foundUser model.Test

	db.Model(&model.Test{}).
		Where("id = ?", 1).
		First(&foundUser)

	fmt.Println(foundUser)

	// 更新数据
	//foundUser.Name = "test"
	//db.Save(&foundUser)

	// 删除数据
	//db.Delete(&foundUser)

	fmt.Println("Database operations done.")

}
